﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ECare.Database;

namespace ECare.BUS.Workers
{
    public class ComputeOutFrmHelper : BackgroundWorker
    {
        public ComputeOutFrmHelper()
        {
            this.DoWork += ComputeOutFrmHelper_DoWork;
        }

        /// <summary>
        /// 没有用了
        /// </summary>
        public int id { get; set; }




        void ComputeOutFrmHelper_DoWork(object sender, DoWorkEventArgs e)
        {


          //  var needed = PlanHelper.GetPlanNeeded(id);

            var findresources = ResourceOutHelper.GetOutINfos("resource_out_id=" + id);

            //需要的数量
            var needednumbers = findresources[0];

            var needed = new Dictionary<int, int>();
            needed.Add(needednumbers.resource_id.Value, needednumbers.resource_numbers.Value);

            var table = new DataTable();


            var supplyColumn = new DataColumn("supply_id", typeof(System.Int32));
            table.Columns.Add(supplyColumn);


            var resourceColumn = new DataColumn("resource_Id", typeof(System.Int32));
            table.Columns.Add(resourceColumn);

            var resourcecodecolumn = new DataColumn("supply_code", typeof(System.String));
            table.Columns.Add(resourcecodecolumn);

            var resourcedetailcodecolumn = new DataColumn("supply_detail_code", typeof(System.String));
            table.Columns.Add(resourcedetailcodecolumn);

            //单位重量
            var resourceDecimalcolumn = new DataColumn("supply_kg", typeof(System.Decimal));
            table.Columns.Add(resourceDecimalcolumn);

            //优先码
            var resourcehignbarcodecolumn = new DataColumn("supply_HignBarcode", typeof(System.String));
            table.Columns.Add(resourcehignbarcodecolumn);

            //supply_detail_number
            var supplydetailColumn = new DataColumn("supply_detail_number", typeof(System.Int32));
            table.Columns.Add(supplydetailColumn);


            var supplyIdColumn = new DataColumn("supply_detail_Id", typeof(System.Int32));
            table.Columns.Add(supplyIdColumn);

            var _validatedateColumn = new DataColumn("supply_invalidate", typeof(System.DateTime));
            table.Columns.Add(_validatedateColumn);




            var result = new ComputeResult { messages = new List<string>() };

            List<string> codes = new List<string>();

            using (var context = new Entities())
            {


                foreach (var need in needed)
                {
                    var neednumber = need.Value;
                    var resourceId = need.Key;

                    var resources = context.getStorageByResourceId(resourceId).ToList();
                    if (resources.Count() == 0)
                    {

                        result.messages.Add(string.Format(" {0} is not enough.", ResourcesHelper.GetResourceName(resourceId)));

                        continue;
                    }

                    int tempnumber = 0;
                    foreach (var resource in resources)
                    {

                        var newrow = table.NewRow();


                        if (!codes.Contains(resource.supply_code))
                            codes.Add(resource.supply_code);


                        newrow["resource_Id"] = resource.resource_Id.Value;
                        newrow["supply_id"] = resource.supply_id.Value;

                        newrow["supply_code"] = resource.supply_code;


                        newrow["supply_detail_code"] = resource.supply_detail_code;

                        newrow["supply_kg"] = resource.supply_kg.Value;
                        newrow["supply_HignBarcode"] = resource.supply_HighBarcode;
                        newrow["supply_detail_number"] = 1;
                        newrow["supply_detail_Id"] = resource.supply_detail_id;

                        if (resource.supply_invalid_time != DateTime.MinValue && resource.supply_invalid_time != null)
                        {
                            newrow["supply_invalidate"] = resource.supply_invalid_time;

                            if (resource.supply_invalid_time < DateTime.Now)
                            {
                                string msg = string.Format("  批号 {0} 的物料 {1} 已经过期.", resource.supply_code,
                                       ResourcesHelper.GetResourceName(resource.resource_Id.Value));
                                if (!result.messages.Contains(msg))
                                    result.messages.Add(msg);


                                result.IsShowReCheckButton = true;
                            }
                        }
                        table.Rows.Add(newrow);
                        tempnumber ++;
                        if (tempnumber >= neednumber)
                        {
                            //Added
                            break;
                        }
                    }
                    if (tempnumber < neednumber)
                    {
                        result.messages.Add(string.Format(" {0} is not enough.", ResourcesHelper.GetResourceName(resourceId)));
                    }

                }
            }
            result.result = table;

            result.result_shown = new DataTable();

            var _resourceColumn = new DataColumn("resource_Id", typeof(System.Int32));
            result.result_shown.Columns.Add(_resourceColumn);

            var _resourcecodecolumn = new DataColumn("supply_code", typeof(System.String));
            result.result_shown.Columns.Add(_resourcecodecolumn);

            var _resourceDecimalcolumn = new DataColumn("supply_kg", typeof(System.Decimal));
            result.result_shown.Columns.Add(_resourceDecimalcolumn);
            var _supplydetailColumn = new DataColumn("supply_detail_number", typeof(System.Int32));
            result.result_shown.Columns.Add(_supplydetailColumn);


            var _resourcetotalDecimalcolumn = new DataColumn("supply_total_kg", typeof(System.Int32));
            result.result_shown.Columns.Add(_resourcetotalDecimalcolumn);


            var _supplyIdcolumn = new DataColumn("supply_id", typeof(System.Int32));
            result.result_shown.Columns.Add(_supplyIdcolumn);


            //有效期
            var validatedateColumn = new DataColumn("supply_invalidate", typeof(System.DateTime));
            result.result_shown.Columns.Add(validatedateColumn);

            foreach (var code in codes)
            {
                var newrow = result.result_shown.NewRow(); newrow["supply_code"] = code;

                var drs = result.result.Select(string.Format("supply_code='{0}'", code));

                if (drs.Length > 0)
                {
                    newrow["resource_Id"] = int.Parse(drs[0]["resource_Id"].ToString());
                    newrow["supply_kg"] = decimal.Parse(drs[0]["supply_kg"].ToString());
                    newrow["supply_id"] = int.Parse(drs[0]["supply_id"].ToString());
                    newrow["supply_detail_number"] = drs.Length;
                    newrow["supply_total_kg"] = decimal.Parse(drs[0]["supply_kg"].ToString()) * drs.Length;
                    newrow["supply_invalidate"] = drs[0]["supply_invalidate"];
                    result.result_shown.Rows.Add(newrow);
                }

            }


            e.Result = result;


        }

    }

    public class ComputeResult
    {
        public List<String> messages { get; set; }
        public DataTable result { get; set; }
        public DataTable result_shown { get; set; }

        public bool IsShowReCheckButton = false;

    }

}
